#ifndef CLIENTHANDLER_H
#define CLIENTHANDLER_H




// preprocessor directives
#include <iostream>
#include <QtCore>
#include <QTcpServer>
#include <QTcpSocket>
#include <QString>
#include <QDataStream>
#include "user.h"

using namespace std;

/*! \brief ClientHandler:
  Handles data from server to client and client to server.
*/

class ClientHandler : public QObject
{
    Q_OBJECT

public:
    /*! ClientHandler
      \brief QVector of Tcp Sockets
      \param
      \param
      */
    ClientHandler(QVector<QTcpSocket *> *theClientsConnected, QTcpSocket *clientSocket);

    /*! \var QString theMessage
      \brief
      */
    QString theMessage;

private slots:
    /*! handleIncomingData
      \brief Handles incoming data.
      */
    void handleIncomingData();

    /*! handleDisconnection
      \brief Handls disconnection.
      */
    void handleDisconnection();

private:

    QString screenName;

    /*! \var QTcpSocket *mySocket
      \brief its a socket
      */
    QTcpSocket *mySocket;

    /*! \var QDataStream data
      \brief
      */
    QDataStream data;

    /*! \var QString tag
      \brief
      */
    QString tag;

    /*! \var QString username
      \brief
      */
    QString username;

    /*! \var QString password
      \brief
      */
    QString password;

    /*! \var QString email
      \brief
      */
    QString email;

    /*! \var QString from
      \brief
      */
    QString from;

    /*! \var QString to
      \brief
      */
    QString to;

    /*! \var QString incomingMessage
      \brief
      */
    QString incomingMessage;

    /*! \var QString outgoingMessage
      \brief
      */
    QString outgoingMessage;

    /*! \var QString status
      \brief
      */
    QString status;

    /*! \var QVector<QTcpSocket *> *allSockets
      \brief
      */
       QVector<QTcpSocket *> *allSockets;

     /*! \var int sesssionId
      \brief
      */
    int sessionId;

    /*! \var quint16 nextBlockSize
      \brief
      */
    quint16 nextBlockSize;


    /*! tagReader
      \brief Reads the tags from the data stream and goes to appropriate function.
      \param theData the data stream
      \param socketId the socket ID of the user
      */
    void tagReader(QDataStream &theData, int socketId);

    /*! getAuthorization
      \brief Authentifies the user and logs them in.
      \param theData the data stream
      \param socketId the socket ID
      \socketId the socket ID
      */
    void getAuthorization(QDataStream &theData, int socketId);

    /*! registerUser
      \brief Registers a user.
      \param theData the data stream
      \param socketId the socket ID
      */
    void registerUser(QDataStream &theData, int socketId);

    /*! getMessage
      \brief Takes data stream and socket ID and sents the message to
      the sender and receiver.
      \param theData the data stream
      \param socketId the socket ID
      */
    void getMessage(QDataStream &theData, int socketId);

    /*! getPresence
      \brief Retrieves the status of a user
      \param theData the data stream
      */
    void getPresence (QDataStream &theData);

    /*! writeToClients
      \brief Writes data to clients.
      */
    void writeToClients();

    /*! writeToBuddyLists
      \brief Writes status of buddies on buddy list.
      \param status the status
      */
    void writeToBuddylists(QString status);

    /*! sendBuddies
      \brief Gets a user's buddies and sends the vector of buddies to the buddy list.
      */
    void sendBuddies();

void getAddContact(QDataStream &theData);
void getRemoveContact(QDataStream &theData);
void sendNoContacts();

};

#endif // CLIENTHANDLER_H
